var stationStandardData = [];
var chargingTimePriceData = [];
var chargingStationData = [];
var deleteFlag = "";

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	createTableHead("stationStandardThead", stationStandardTable);
	$ajaxList('chargingTimePriceName', getChargingTimePriceName);
	$ajaxList('chargingStation', getChargingStation, {BulidStatus: '3'});
	search();
});
function getChargingTimePriceName(data) {
	chargingTimePriceData = data || [];
}
function getChargingStation(data) {
	chargingStationData = data;
}
function search(){
	$ajax('searchStationStandard', '', searchStationStandard);
}

function searchStationStandard(data){
	stationStandardData = data && data.stationStandardList || [];
	createTableBody("stationStandardTbody", stationStandardTable, stationStandardData, false, true);
}

function save() {
	deleteFlag = "save";
	addTitle("stationStandardSave");
	allTable();
}
function chkBefore(index, msg){
	deleteFlag = msg;
	$("#ID").val(stationStandardData[index].id);
	if (deleteFlag == "update") {
		addTitle("stationStandardUpdate");
		allTable(index);
	} else if (deleteFlag == "delete") {
		showMessge(messageContent["Delete.Confirm"], "model2");
	}
}
function allTable(index){
	createComBody("detailOneBody", stationStandardTable, stationStandardData[index], 1);
	for(var i = 0; i < chargingTimePriceData.length; i++) {
		$("#priceType").append("<option value='" + chargingTimePriceData[i].priceType + "'>" + chargingTimePriceData[i].priceType + "</option>");
	}
	for (var i = 0;i < chargingStationData.length;i++){
        $("#chargingStationID").append("<option value='" + chargingStationData[i].chargingStationID + "'>" + chargingStationData[i].chargingStationName + "</option>");  
	}
	if(deleteFlag == "update") {
		$("#chargingStationID").val(stationStandardData[index].chargingStationID);
		$("#powerFactor").val(stationStandardData[index].powerFactor);
		$("#priceType").val(stationStandardData[index].priceType);
		$("#factorStandard").val(stationStandardData[index].factorStandard);
	} else {
		$("#addUnitPrice").val(0.0304);
		$("#priceType").val("1");
		$("#powerFactor").val("0");
		$("#factorStandard").val("0");
	}
	$("#save").modal("show");
}

function getUrl(){
	if(deleteFlag == "save"){
		return 'saveStationStandard';
	} else if(deleteFlag == "update"){
		return 'updateStationStandard';
	} else if(deleteFlag == "delete"){
		return 'deleteStationStandard';
	}
}

function confirm(){
	return checkForm("saveForm", stationStandardTable);
}

function saveStationStandard(data){
	if(deleteFlag == "save"){
	     var result = data && data.result;
	     if (result == "Success") {
	    	 showMessge(messageContent["Insert.Success"]);
	    	 $('#save').modal('hide');
	    	 formReset("saveForm");
	     } else if (result == "NotFound"){
	    	 showMessge(messageContent["Insert.Failed"]);
	     } else {
	    	 showMessge(messageContent["Insert.Error"]);
	     }
	     search();
	}else if(deleteFlag == "update"){
	     var result = data && data.result;
	     if (result == "Success"){
	    	 showMessge(messageContent["Update.Success"]);
	    	 $('#save').modal('hide');
	    	 formReset("saveForm");
	     } else if (result == "NotFound"){
	    	 showMessge(messageContent["Update.Failed"]);
	     } else{
	    	 showMessge(messageContent["Update.Error"]);
	     }
	     search();
	}
}
function getData(){
	return {ID: $("#ID").val()};
}
function sure(data){
	if(deleteFlag == "delete"){
		var result = data && data.result;
	    if (result == "Success") {
			showMessge(messageContent["Delete.Success"]);
		} else if (result == "NotFound") {
			showMessge(messageContent["Delete.NotFound"]);
		} else {
			showMessge(messageContent["Delete.Error"]);
		}
	    search();
	} 
}